// utils/request.js
const BASE_URL = 'http://127.0.0.1:4523/m1/7173039-6897305-default'

export const request = (options) => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      header: {
        'Content-Type': 'application/json',
        'Authorization': uni.getStorageSync('token') || '',
        ...options.header
      },
      success: (res) => {
        // 根据文档，成功响应码是200
        if (res.statusCode === 200 || res.data.code === 200) {
          resolve(res.data)
        } else {
          uni.showToast({
            title: res.data.message || `请求失败 (${res.statusCode})`,
            icon: 'none'
          })
          reject(res.data)
        }
      },
      fail: (err) => {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
        reject(err)
      }
    })
  })
}

// API 接口统一导出 - 根据文档修正
export const api = {
  // 用户认证 - 文档中只有登录接口
  login: (data) => request({ url: '/api/login', method: 'POST', data }),
  
  // 课程管理
  getCourseSchedule: (params) => request({ 
    url: '/api/course-schedule', 
    method: 'GET', 
    data: params 
  }),
  
  // 教室管理
  getClassrooms: () => request({ url: '/api/classroom', method: 'GET' }),
  reserveClassroom: (data) => request({ 
    url: '/api/reservation', 
    method: 'POST', 
    data 
  }),
  
  // 签到管理
  createCheckinSession: (data) => request({ 
    url: '/api/checkin-session', 
    method: 'POST', 
    data 
  }),
  checkin: (data) => request({ 
    url: '/api/checkin', 
    method: 'POST', 
    data 
  }),
  
  // 通知管理
  getNotifications: () => request({ 
    url: '/api/notification', 
    method: 'GET' 
  }),
  publishNotification: (data) => request({ 
    url: '/api/notification', 
    method: 'POST', 
    data 
  })
}